from flask import Blueprint
from exts import db
from flask import Flask, render_template,flash
from flask import request
from models import DataModel
#from forms import JudgeForm验证器，这里不需要
from flask import Flask, request, jsonify 
bp = Blueprint("calculate",__name__,url_prefix="/calculate")

@bp.route("/",methods = ['POST','GET'])
def calculate():
    if request.method == 'GET':
        return  render_template("calculate.html")
    else:
      # # 定义触发条件
        threshold = 1000 * 0.05  # 增量 >= 5%
        data1 = float(request.form.get('data1', 0))  
        data2 = float(request.form.get('data2', 0))  
        data3 = float(request.form.get('data3', 0)) 
        data4 = float(request.form.get('data4', 0)) 
        data5 = float(request.form.get('data5', 0)) 
        data6 = float(request.form.get('data6', 0)) 
        data7 = float(request.form.get('data7', 0)) 
        data8 = float(request.form.get('data8', 0)) 
        data9 = float(request.form.get('data9', 0)) 
        data10 = float(request.form.get('data10', 0)) 
        data11 = float(request.form.get('data11', 0)) 
        data12 = float(request.form.get('data12', 0)) 
        # 定义初始值
        initial_values = {
            'H1-1': 1000, 'H1-2': 1000, 'Z1-1': 1000, 'Z1-2': 1000,
            'H2-1': 1000, 'H2-2': 1000, 'Z2-1': 1000, 'Z2-2': 1000,
            'H3-1': 1000, 'H3-2': 1000, 'Z3-1': 1000, 'Z3-2': 1000
        }

        # 定义触发条件
        
        data=[]
        data.append([data1,data2,data3,data4])
        data.append([data5,data6,data7,data8])
        data.append([data9,data10,data11,data12])
        print(data)
        
        
        area = [0, 0, 0, 0]

        # 检查每个区域是否异常

        # 判断A1区域是否异常
        # 判断A1区域是否异常
        abnormal_count = 0
        if abs(data[0][0]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[0][2]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][0]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][2]-1000) >= threshold:
            abnormal_count += 1
        if abnormal_count >= 3:
            area[0] = 1

            Data=DataModel(data1=data[0][0],data2=data[0][1],data3=data[0][2],data4=data[0][3],data5=data[1][0],data6=data[1][1],data7=data[1][2],data8=data[1][3],data9=data[2][0],data10=data[2][1],data11=data[2][2],data12=data[2][3],结果 = 'A1区域发生异常')
            db.session.add(Data)
            db.session.commit()
            # return render_template('A1.html')
            # return render_template('incorrect.html',area='A1区域')
            # print("A1区域发生异常")

        # 判断A2区域是否异常
        abnormal_count = 0
        if abs(data[0][1]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][1]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][2]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[2][2]-1000) >= threshold:
            abnormal_count += 1
        if abnormal_count >= 3:
            area[1] = 1

            Data=DataModel(data1=data[0][0],data2=data[0][1],data3=data[0][2],data4=data[0][3],data5=data[1][0],data6=data[1][1],data7=data[1][2],data8=data[1][3],data9=data[2][0],data10=data[2][1],data11=data[2][2],data12=data[2][3],结果 = 'A2区域发生异常')
            db.session.add(Data)
            db.session.commit()
            # return render_template('incorrect.html',area='A2区域')
        
            # print("A2区域发生异常")

        # 判断A3区域是否异常
        abnormal_count = 0
        if abs(data[0][3]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][0]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][3]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[2][0]-1000) >= threshold:
            abnormal_count += 1
        if abnormal_count >= 3:
            area[2] = 1
    
            Data=DataModel(data1=data[0][0],data2=data[0][1],data3=data[0][2],data4=data[0][3],data5=data[1][0],data6=data[1][1],data7=data[1][2],data8=data[1][3],data9=data[2][0],data10=data[2][1],data11=data[2][2],data12=data[2][3],结果 = 'A3区域发生异常')
            db.session.add(Data)
            db.session.commit()
            # return render_template('incorrect.html',area='A3区域')
            # print("A3区域发生异常")

        # 判断A4区域是否异常
        abnormal_count = 0
        if abs(data[1][1]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[1][3]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[2][1]-1000) >= threshold:
            abnormal_count += 1
        if abs(data[2][3]-1000) >= threshold:
            abnormal_count += 1
        if abnormal_count >= 3:
            area[3] = 1

            Data=DataModel(data1=data[0][0],data2=data[0][1],data3=data[0][2],data4=data[0][3],data5=data[1][0],data6=data[1][1],data7=data[1][2],data8=data[1][3],data9=data[2][0],data10=data[2][1],data11=data[2][2],data12=data[2][3],结果 = 'A4区域发生异常')
            db.session.add(Data)
            db.session.commit()
            # return render_template('incorrect.html',area='A4区域')
            # print("A4区域发生异常")
        print('haihaihaihai')
        print(area)
        if area[0]==0 and area[1]==0 and area[2]==0 and area[3]==0:
            Data=DataModel(data1=data[0][0],data2=data[0][1],data3=data[0][2],data4=data[0][3],data5=data[1][0],data6=data[1][1],data7=data[1][2],data8=data[1][3],data9=data[2][0],data10=data[2][1],data11=data[2][2],data12=data[2][3],结果 = '无异常')
            db.session.add(Data)
            db.session.commit()
            return render_template('correct.html')
        
        if area[0] == 1 and area[1] == 0 and area[2] == 0 and area[3] == 0:  
            return render_template('A1.html')  
        if area[0] == 0 and area[1] == 1 and area[2] == 0 and area[3] == 0:  
            return render_template('A2.html')  
        if area[0] == 0 and area[1] == 0 and area[2] == 1 and area[3] == 0:  
            return render_template('A3.html')  
        if area[0] == 0 and area[1] == 0 and area[2] == 0 and area[3] == 1:  
            return render_template('A4.html')  
        if area[0] == 1 and area[1] == 1 and area[2] == 0 and area[3] == 0:  
            return render_template('A12.html')  
        if area[0] == 1 and area[1] == 0 and area[2] == 1 and area[3] == 0:  
            return render_template('A13.html')  
        if area[0] == 1 and area[1] == 0 and area[2] == 0 and area[3] == 1:  
            return render_template('A14.html')  
        if area[0] == 0 and area[1] == 1 and area[2] == 1 and area[3] == 0:  
            return render_template('A23.html')  
        if area[0] == 0 and area[1] == 1 and area[2] == 0 and area[3] == 1:  
            return render_template('A24.html')  
        if area[0] == 0 and area[1] == 0 and area[2] == 1 and area[3] == 1:  
            return render_template('A34.html')  
        if area[0] == 1 and area[1] == 1 and area[2] == 1 and area[3] == 0:  
            return render_template('A123.html')  
        if area[0] == 1 and area[1] == 1 and area[2] == 0 and area[3] == 1:  
            return render_template('A124.html')  
        if area[0] == 1 and area[1] == 0 and area[2] == 1 and area[3] == 1:  
            return render_template('A134.html')  
        if area[0] == 0 and area[1] == 1 and area[2] == 1 and area[3] == 1:  
            return render_template('A234.html')  
        if area[0] == 1 and area[1] == 1 and area[2] == 1 and area[3] == 1:  
            return render_template('A1234.html')
    print('area is ')
    print(area)
    print(area[0]==1)
    print(area[1]==1)
    print(area[2]==0)
    print(area[3]==0)